package eud.zbjs.dragonet.android_;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity2 extends AppCompatActivity implements View.OnClickListener {
    private Boolean pd1 = true;
    private Boolean jushu = false;
    //判斷是不是第一位玩家
    private int sz [][] = new int[3][3];
    //用一個二位數組標記棋盤
    private int js = 0;
    private int[] abc;

    private TextView Mtv_js;
    int Asf = 0; // A玩家计分器
    int Bsf = 0; // B玩家计分器
    private Button[] bt  = new Button[9];
    //計數器

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); //隐藏状态栏
        setContentView(R.layout.activity_main2);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        }); //全屏模式



        for(int i = 0; i < 9; i++){
            bt[i] = findViewById(getResources().getIdentifier("button" + (i + 1), "id", getPackageName())); //用getResources方法活获取ID
            bt[i].setOnClickListener(this);
        }
        Mtv_js = findViewById(R.id.tv_js);
        findViewById(R.id.bt_cx).setOnClickListener(this);
        findViewById(R.id.bt_fh).setOnClickListener(this);
        //找到按鈕ID並監聽是否按下按鈕

        chushihua(); //初始化數組

    }

    @Override
    public void onClick(View view) {
        //按下按鈕
        switch (view.getId()){
            case R.id.button1:
                if(pd3(0,0)){ //判斷數組是否有内容
                    if(pd1){ //判斷第幾位玩家
                        sz[0][0] = 1;
                    }else{
                        sz[0][0] = 2;
                    }
                    pd2(view);
                }
                break;
            case R.id.button2:
                if(pd3(0,1)){
                    if(pd1){
                        sz[0][1] = 1;
                    }else{
                        sz[0][1] = 2;
                    }
                    pd2(view);
                }
                break;
            case R.id.button3:
                if(pd3(0,2)){
                    if(pd1){
                        sz[0][2] = 1;
                    }else{
                        sz[0][2] = 2;
                    }
                    pd2(view);
                }
                break;
            case R.id.button4:
                if(pd3(1,0)){
                    if(pd1){
                        sz[1][0] = 1;
                    }else{
                        sz[1][0] = 2;
                    }
                    pd2(view);
                }
                break;
            case R.id.button5:
                if(pd3(1,1)){
                    if(pd1){
                        sz[1][1] = 1;
                    }else{
                        sz[1][1] = 2;
                    }
                    pd2(view);
                }
                break;
            case R.id.button6:
                if(pd3(1,2)){
                    if(pd1){
                        sz[1][2] = 1;
                    }else{
                        sz[1][2] = 2;
                    }
                    pd2(view);
                }
                break;
            case R.id.button7:
                if(pd3(2,0)){
                    if(pd1){
                        sz[2][0] = 1;
                    }else{
                        sz[2][0] = 2;
                    }
                    pd2(view);
                }
                break;
            case R.id.button8:
                if(pd3(2,1)){
                    if(pd1){
                        sz[2][1] = 1;
                    }else{
                        sz[2][1] = 2;
                    }
                    pd2(view);
                }
                break;
            case R.id.button9:
                if(pd3(2,2)){
                    if(pd1){
                        sz[2][2] = 1;
                    }else{
                        sz[2][2] = 2;
                    }
                    pd2(view);
                }
                break;
            case R.id.bt_cx:
                chushihua();
                break;
            case R.id.bt_fh:
                finish();
                break;
        }
    }

    private boolean pd2 (View v){
        //判斷是不是第一位玩家如果是返回true並在按鈕上顯示O 如果不是返回false 在按鈕上顯示X
        if(pd1 == true){
            pd1 = false;
            js++;
            chognzhiqipan(v);
            if(Victory_judgment() == 1){
                Asf++;
                Mtv_js.setText(Asf + " v " + Bsf);
                Toast.makeText(this, Victory_judgment()+"号玩家胜利", Toast.LENGTH_SHORT).show();
                for(int i = 0; i < 3; i++){
                    for(int l = 0; l < 3; l++){
                        sz[i][l] = 4;
                    }
                }
            }else if(Victory_judgment() == 3 || js == 9){
                Toast.makeText(this, "平局", Toast.LENGTH_SHORT).show();
                for(int i = 0; i < 3; i++){
                    for(int l = 0; l < 3; l++){
                        sz[i][l] = 4;
                    }
                }
            }
            return true;
        }else{
            pd1 = true;
            js++;
            chognzhiqipan(v);
            if(Victory_judgment() == 2){
                Bsf++;
                Mtv_js.setText(Asf + " v " + Bsf);
                Toast.makeText(this, Victory_judgment()+"号玩家胜利", Toast.LENGTH_SHORT).show();
                for(int i = 0; i < 3; i++){
                    for(int l = 0; l < 3; l++){
                        sz[i][l] = 4;
                    }
                }
            }else if(Victory_judgment() == 3 || js == 9){
                Toast.makeText(this, "平局", Toast.LENGTH_SHORT).show();
                for(int i = 0; i < 3; i++){
                    for(int l = 0; l < 3; l++){
                        sz[i][l] = 4;
                    }
                }
            }
            Victory_judgment();
            return false;
        }
    }

    private boolean pd3(int i, int l){
        //判斷數組裏面是否被點擊過
        if(sz[i][l] == 0){
            return true;
        }else{
            return false;
        }
    }

    private void chushihua(){
        for(int i = 0; i < 3; i++){
            for(int l = 0; l < 3; l++){
                sz[i][l] = 0;
            }
        } //初始化數組
        for(int i = 0; i < 9; i++){
            bt[i].setText("");
        } //初始化棋盘
        js = 0;
        if(jushu){
            jushu = false;
            pd1 = false;
        }else{
            jushu = true;
            pd1 = true;
        }
    }

    public int Victory_judgment(){ //胜利判断
        abc = new int[3];

        //橫列判斷
        for(int i = 0; i < 3; i++) {
            for (int l = 0; l < 3; l++) {
                abc[l] = sz[i][l];
            }
            if(apd()){
                qingchu();
                return 1;
            }else if(bpd()){
                qingchu();
                return 2;
            }
            qingchu();
        }
        //數列判斷
        for(int i = 0; i < 3; i++) {
            for (int l = 0; l < 3; l++) {
                abc[l] = sz[l][i];
            }
            if(apd()){
                qingchu();
                return 1;
            }else if(bpd()){
                qingchu();
                return 2;
            }
            qingchu();
        }
        //右斜向判斷
        for(int i = 0; i < 3; i++){
            for(int l = 2; l >= 0; l--){
                abc[i] = sz[i][l];
                i++;
            }
            if(apd()){
                qingchu();
                return 1;
            }else if(bpd()){
                qingchu();
                return 2;
            }
            qingchu();
        }
        //左斜向判断
        for(int i = 0; i < 3; i++){
            for(int l = 0; l < 3; l++){
                abc[i] = sz[i][l];
                i++;
            }
            if(apd()){
                qingchu();
                return 1;
            }else if(bpd()){
                qingchu();
                return 2;
            }
            qingchu();
        }
        if(js == 9){
            return 3;
        }
        return 0;
    }
    private boolean apd(){
        if(abc[0] == 1 && abc[1] == 1 && abc[2] == 1){
            return true;
        }
        return false;
    }
    private boolean bpd(){
        if (abc[0] == 2 && abc[1] == 2 && abc[2] == 2){
            return true;
        }
        return false;
    }

    public void qingchu(){ //清楚对比数组
        for(int i = 0; i < 3; i++){
           abc [i] =  i;
        }
    }

    public void chognzhiqipan(View v){    //棋盘刷新
        int js = 0;
        for(int i = 0; i < 3; i++){
            for(int l = 0; l < 3; l++){
                if(sz[i][l] == 1){
                    Button button = bt[js];
                    button.setText("O");
                    // 设置文字颜色为粉色
                    String pinkColorHex = "#FFC0CB"; // 粉色的十六进制颜色代码
                    int pinkColor = Color.parseColor(pinkColorHex);
                    button.setTextColor(pinkColor);
                    js++;
                }else if(sz[i][l] == 2){
                    Button button = bt[js];
                    button.setText("X");
                    // 设置文字颜色为青色
                    String pinkColorHex = "#00FFFF"; // 青色的十六进制颜色代码
                    int pinkColor = Color.parseColor(pinkColorHex);
                    button.setTextColor(pinkColor);
                    js++;
                }else{
                    Button button = bt[js];
                    button.setText("");
                    js++;
                }
            }
        }
    }
}